/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package triploa;

import javax.microedition.rms.*;
import javax.microedition.rms.RecordStoreNotFoundException;

/**
 *
 * @author User
 */
public class CacheManager {

    RecordStore options;
    RecordStore login;
    

    /**
     * 
     * @throws javax.microedition.rms.RecordStoreException
     */
    public void CacheManager() throws RecordStoreException {
        
               
    }

    /**
     * Call for reading the saved options for the user,example: save passord and auto login
     * @return saved options
     */
    public Options getOptions()  {
        System.out.println("Richiamato getOptions");
        try{
        Options op = new Options();
        options = RecordStore.openRecordStore("options", true,RecordStore.AUTHMODE_PRIVATE,true);
        if(options.getNumRecords()<2)
        {
            System.out.println("Pochi record "+options.getNumRecords());
            return op;
        }
        RecordEnumeration re = options.enumerateRecords(null, null, false);
        //ritorna la lista dall'ultimo inserito al primo ovvero al contrario rispetto al set
        int id;
        byte[] b;
        /*if (re.hasNextElement()) {
            id = re.nextRecordId();
             b= options.getRecord(id);
            op.serverUrl = new String(b);
        }*/
        if (re.hasNextElement()) {
            id = re.nextRecordId();
            b = options.getRecord(id);
            String s = new String(b);
            
            if (s.equals(t)) {
                op.savePassword = true;
            }
        }
        if (re.hasNextElement()) {
            id = re.nextRecordId();
            b = options.getRecord(id);
            String s = new String(b);
            if (s.equals(t)) {
                op.autoLogin = true;
            }
        }
        options.closeRecordStore();
        options=null;
        return op;
        }catch(Exception e){
            System.out.println(e);
            return new Options();
        }
    }
    String t="T",f="F";
    /**
     * save the options
     * @param op Options
     * @return
     */
    public boolean setOptions(Options op) {
        System.out.println("Richiamato Set Options");
        try{
            if(options!=null)
                options.closeRecordStore();
            try{
                RecordStore.deleteRecordStore("options");
            }catch(RecordStoreNotFoundException a)
            {
            }
            options = RecordStore.openRecordStore("options", true,RecordStore.AUTHMODE_PRIVATE,true);
            String s=f;
            byte[] b;
            if(op.autoLogin)
            {
               s=t;
            }
            b=s.getBytes();
             options.addRecord(b, 0, b.length);
             
             s=f;
            if(op.savePassword)
            {
               s=t;
               System.out.println("scrittura savapassword true");
            }
             b=s.getBytes();
             options.addRecord(b, 0, b.length);
             /*(op.serverUrl==null)
             {
                 op.serverUrl="";
                 
             }
             //b=op.serverUrl.getBytes();
             //options.addRecord(b, 0, b.length);*/
             System.out.println("Record Dopo Scrittura "+options.getNumRecords());
             options.closeRecordStore();
             options=null;
             
        }catch(Exception e){
            System.out.println(e);
        return false;}
        return true;
    }
    String username=null;
    String password=null;
    /**
     * Read login information from recordStore e save it to username e password filds
     * @return
     */
    public boolean loadLogin() {
        //loading username e password
        try{
            
        login = RecordStore.openRecordStore("Login", true,RecordStore.AUTHMODE_PRIVATE,true);
        RecordEnumeration re = login.enumerateRecords(null, null, false);
        //ritorna la lista dall'ultimo inserito al primo
        int id=0;
        if(login.getNumRecords()>2){
            /*if(login.getNumRecords()==1||login.getNumRecords()>2){
                login.closeRecordStore();
                RecordStore.deleteRecordStore("Login");
                return false;
            }*/
            System.out.println("Troppi dati login "+login.getNumRecords());
            return false;
        }
        if (re.hasNextElement()) {
            id=re.nextRecordId();
            password=new String(login.getRecord(id));
        }
        if (re.hasNextElement()) {
            id=re.nextRecordId();
            username=new String(login.getRecord(id));
        }
        login.closeRecordStore();
        login=null;
        }catch(Exception e)
        {
            System.out.println("Errore dentro cache manager "+e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 
     * @return saved Username
     */
    public String getLoginUsername() {
        return username;
    }

    /**
     * 
     * @return saved Password
     */
    public String getLoginPassword() {
        return password;
    }
    
    /**
     * save login information to recordstore
     * @param username
     * @param password
     */
    public void setLogin(String username,String password)
    {
        try{
            if(login!=null)
                login.closeRecordStore();
            try{
                RecordStore.deleteRecordStore("Login");
            
            }catch(RecordStoreNotFoundException a)
            {
            }
            login = RecordStore.openRecordStore("Login", true,RecordStore.AUTHMODE_PRIVATE,true);
             login.addRecord(username.getBytes(), 0, username.getBytes().length);
             login.addRecord(password.getBytes(), 0, password.getBytes().length);
             login.closeRecordStore();
             login=null;
        }catch(Exception e)
        {
            System.out.println("Errore scrittura password "+e.getMessage());
        }
    }
    
}
